package com.iwop.moto.controller.web;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.MobomApplyService;
import com.iwop.moto.utils.stringUtil.ExportUtil;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.weixin.WeixinUtil;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能: 开摩邦活动报名
 * 数据表: B_MOBOM_APPLY
 * @author  Created by liuwb on 2016-10-19 14:41:56
 */
@Controller
public class MobomApplyController extends BaseController {


    @Autowired
    private MobomApplyService mobomApplyService;

    /**
     * @功能:永春发布会邀请函
     * @日期:liuwb by 2016-10-19 15:04:49 create
     * @return
     */
    @RequestMapping(value="/api/mobom/apply/yc")
    public ModelAndView motolist() {
        ModelAndView mv = new ModelAndView();
        try{
            mv.addObject("ticket",getWxTicket().get("ticket"));
            mv.setViewName("apply");
        }catch (Exception e){
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * @功能: 新增活动报名人员
     * @日期: liuwb by 2016-10-19 14:46:04 create
     * @参数: apply_type 类型
     * @参数: apply_explain 描述
     * @参数: apply_real_name 姓名
     * @参数: apply_tel 手机号
     */
    @RequestMapping(value="/api/mobom/apply")
    @ResponseBody
    public ResultModel apiMobomApply() {
        SuperRecordModel reqParams = this.getSRModel();
        if(reqParams.get("apply_type")==null || reqParams.get("apply_explain") == null ||
              reqParams.get("apply_real_name") == null || reqParams.get("apply_real_name")==null){
            return getErr(ErrCode.ERROR_PARAMETER);
        }
        try {
            if(mobomApplyService.findTelIsApply(reqParams)==0l){
                reqParams.put("sid", SequenceUtil.Serial("BM"));
                reqParams.put("status_flag","1");
                reqParams.put("status_time", new Date());
                mobomApplyService.insert(reqParams);
                return getSucceed(ErrCode.SUCCEED);
            }else{
                return getErr(ErrCode.TEL_IS_APPLY);
            }
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


    /**
     * @功能: 导出报名
     * @日期: gewei by 2016-10-24 15:06:19 create
     * @参数: apply_type 活动类型
     * @param response
     * @return
     * @throws IOException
     * @throws WriteException
     */
    @RequestMapping(value="/api/mobom/apply/export")
    @ResponseBody
    public ResultModel apiMobomExport(HttpServletResponse response) throws IOException, WriteException {
        SuperRecordModel reqParams = this.getSRModel();
        String title[] = {"姓名","手机号","报名时间"};
        List<SuperRecordModel> listByParam = mobomApplyService.findListByParam(reqParams);
        List lists=new ArrayList<>();
        response.reset();
        response.setContentType("octets/stream");
        String xlsName="用户报名";
        if (listByParam!=null){
            xlsName=listByParam.get(0).getString("apply_explain");
            for (SuperRecordModel superRecordModel:listByParam){
                List<Object> cusList=new ArrayList<>();
                cusList.add(superRecordModel.getString("apply_real_name"));
                cusList.add(superRecordModel.getString("apply_tel"));
                cusList.add(superRecordModel.get("status_time"));
                lists.add(cusList);
            }
        }
        String fileName =xlsName+ ".xls";
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
        OutputStream outputStream = response.getOutputStream();
        ExportUtil.writeExcel(outputStream, title, lists, xlsName);
        return getSucceed(null);
    }

}
